/*
 * @Author: wuqingchun
 * @Date: 2024-08-01 21:08:24
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-08-01 21:42:30
 */
#ifndef __LIST_H__
#define __LIST_H__

#include <stdio.h>

struct list_head
{
    struct list_head* pstPrev;
    struct list_head* pstNext;
};


void list_init(struct list_head* pstHead)
{
    pstHead->pstNext = pstHead;
    pstHead->pstPrev = pstHead;
}


static inline void __list_add(struct list_head* pstPrev, struct list_head* pstNew, struct list_head* pstNext)
{
    pstNew->pstNext = pstNext;
    pstNew->pstPrev = pstPrev;
    pstNext->pstPrev = pstNew;
    pstPrev->pstNext = pstNew;
}

static inline void __list_del(struct list_head* pstPrev, struct list_head* pstDel, struct list_head* pstNext)
{
    pstPrev->pstNext = pstNext;
    pstNext->pstPrev = pstPrev;

    pstDel->pstNext = NULL;
    pstDel->pstPrev = NULL;
}

static inline void list_add(struct list_head* pstHead, struct list_head* pstNew)
{
    __list_add(pstHead, pstNew, pstHead->pstNext);
}

static inline void list_add_tail(struct list_head* pstHead, struct list_head* pstNew)
{
    __list_add(pstHead->pstPrev, pstNew, pstHead);
}

static inline void list_del(struct list_head* pstDel)
{
    __list_del(pstDel->pstPrev, pstDel, pstDel->pstNext);
}

static inline struct list_head* list_del_tail(struct list_head* pstHead)
{
    if (pstHead->pstNext == pstHead)
    {
        return NULL;
    }

    struct list_head* pstNode = pstHead->pstPrev;
    __list_del(pstNode->pstPrev, pstNode, pstNode->pstNext);
    return pstNode;
}

#endif //__LIST_H__